package CompletableFuture;

import java.util.concurrent.*;

/**
 * @author chaird
 * @create 2022-03-30 9:38
 */
public class TeaForCountDownLatch {
  public static void main(String[] args) throws Exception{

      ExecutorService executorService = Executors.newFixedThreadPool(2);
      CountDownLatch countDownLatch = new CountDownLatch(2);
      Future<String> task1Future = executorService.submit(new Callable<String>() {
          @Override
          public String call() {
              System.out.println("洗水壶、烧开水开始（1+15分钟）-----");
              try {
                  TimeUnit.SECONDS.sleep(5);
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
              }
              System.out.println("洗水壶、烧开水结束（1+15分钟）-----");
              countDownLatch.countDown();
              return "task 1 ok";
          }
      });

      Future<String> task2Future = executorService.submit(new Callable<String>() {
          @Override
          public String call() {
              System.out.println("洗茶壶、洗茶杯、拿茶叶开始（1+2 +1分钟）-----");
              try {
                  TimeUnit.SECONDS.sleep(3);
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
              }
              System.out.println("洗茶壶、洗茶杯、拿茶叶结束（1+2 +1分钟）-----");
              countDownLatch.countDown();
              return "task 2 ok";
          }
      });


      countDownLatch.await();

    System.out.println("主线程开始泡茶：" + task1Future.get()  + "---" + task2Future.get());
    System.out.println("主线程结束泡茶：" + task1Future.get()  + "---" + task2Future.get());
  }
}
